<?php

define("ARCEMU_ACCOUNT_FLAGS_ORIGINAL", 0);
define("ARCEMU_ACCOUNT_FLAGS_TBC", 8);
define("ARCEMU_ACCOUNT_FLAGS_WOTLK", 16);

class ArcemuAccount implements IAccount
{
	private $db = null;
	
	private $info;
	
	public function __construct($db, $login)
	{
		$this->db =& $db;
		
		$q = $this->db->query("SELECT * FROM {$this->db->arcemu_logon_db}.accounts WHERE login = '" . $this->db->escape($login) . "'");
		
		if($this->db->numRows($q) != 1)
		{
			return false;
		}
		
		$this->info = $this->db->fetchArray($q, MYSQL_ASSOC);
		
		return true;
	}
	
	public function getInfo($key)
	{
		return $this->info[$key];
	}
	
	public function setPassword($passwd)
	{
		$passwd = sha1(md5($this->getInfo('login')).":".md5($passwd));
		$q = $this->db->query(
			"UPDATE `{$this->db->arcemu_logon_db}`.`accounts` 
			SET encrypted_password = '" . $this->db->escape($passwd) . "' 
			WHERE id = '" . $this->getInfo("id") . "'" );
		
		if($q)
		{
			return true;
		}
		
		return false;
	}
	
	public function getAccountFlags()
	{
		return $this->getInfo("flags");
	}
	
	public function setAccountFlags($flags)
	{
		if(!is_numeric($flags))
		{
			return false;
		}
		
		$q = $this->db->query(
			"UPDATE `{$this->db->arcemu_logon_db}`.`accounts` 
			SET encrypted_password = '" . $this->db->escape($flags) . "' 
			WHERE id = '" . $this->getInfo("id") . "'"
		);
		
		if($q)
		{
			return true;
		}
		
		return false;
	}
	
	public function getAccountPrivileges()
	{
		
	}
	
	public function setAccountPrivileges($priv)
	{
		return $priv;
	}
	
	public function ban($time = null)
	{
		$q = $this->db->query(
			"UPDATE `{$this->db->arcemu_logon_db}`.`accounts`
			SET banned = '" . $this->db->escape($time) . "'"
		);
		
		if(!$q)
		{
			return false;
		}
		
		return true;
	}
	
	public function unban()
	{
		$q = $this->db->query(
			"UPDATE `{$this->db->arcemu_logon_db}`.`accounts`
			SET banned = null
			WHERE id = '" . $this->getInfo('id') . "'"
		);
		
		if(!$q)
		{
			return false;
		}
		
		return true;
	}
	
	public function mute()
	{
		
	}
	
	public function unmute()
	{
		
	}
	
	public function getCharacterList()
	{
		$q = $this->db->query(
			"SELECT guid, name, level, race, class 
			FROM `{$this->db->arcemu_char_db}`.`characters` 
			WHERE ownerguid = '{$this->getInfo('id')}'"
		);
		
		if(!$q)
		{
			return false;
		}
		
		$chars = array();
		
		while(($char = $this->db->fetchArray($q, MYSQL_ASSOC)))
		{
			$chars[] = $char;
		}
		
		return $chars;
	}
	
	public function getCharacter($guid)
	{
		return $guid;
	}
}

?>